package com.galaxy.commom.base.filter;

import com.galaxy.commom.base.utils.DateUtil;
import com.galaxy.commom.base.utils.IpUtil;
import java.io.IOException;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * CSRF过滤器
 * @author caijinbang
 * @date 2018/9/8 21:27
 */
@Slf4j
public class CsrfFilter implements Filter {

  /**
   * 白名单
   */
  private List<String> whiteUrls;

  private int size = 0;

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    // 读取文件
    String path = CsrfFilter.class.getResource("/").getFile();
    //whiteUrls = FileUtil.readUtf8Lines(path + "csrf-white.txt");
    size = null == whiteUrls ? 0 : whiteUrls.size();
  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    // 获取请求url地址
    String url = req.getRequestURL().toString();
    String referurl = req.getHeader("Referer");
    if (isWhiteReq(referurl)) {
      filterChain.doFilter(request, response);
    } else {
      req.getRequestDispatcher("/").forward(req, res);
      StringBuilder logSb = new StringBuilder();
      String clientIp = IpUtil.getIpAddress(req);
      // 记录跨站请求日志
      logSb.append("跨站请求---->>>").append(clientIp).append("||").append(DateUtil.getCurrentDateTime()).append("||").append(referurl)
          .append("||").append(url);
      //TODO 使用消息中间件记录日志
      log.warn(logSb.toString());
    }
  }

  @Override
  public void destroy() {

  }

  /**
   * 判断是否是白名单
   * @param referUrl
   * @return
   */
  private boolean isWhiteReq(String referUrl) {
    if (referUrl == null || "".equals(referUrl) || size == 0) {
      return true;
    } else {
      String refHost = "";
      referUrl = referUrl.toLowerCase();
      String httpUrlPrefix = "http://";
      String httpsUrlPrefix = "https://";
      if (referUrl.startsWith(httpUrlPrefix)) {
        refHost = referUrl.substring(7);
      } else if (referUrl.startsWith(httpsUrlPrefix)) {
        refHost = referUrl.substring(8);
      }
      for (String urlTemp : whiteUrls) {
        if (refHost.contains(urlTemp.toLowerCase())) {
          return true;
        }
      }
    }
    return false;
  }
}
